package com.yuke.cloud.service.cmc.controller.frontend;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yuke.cloud.common.core.support.BaseController;
import com.yuke.cloud.common.util.wrapper.WrapMapper;
import com.yuke.cloud.common.util.wrapper.Wrapper;
import com.yuke.cloud.service.cmc.entity.CmsCoupon;
import com.yuke.cloud.service.cmc.service.CmsCouponService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
* Created by wg on 2019/09/27.
*/
@Api(value = "WEB - CmsCouponController", tags = "处理CmsCoupon相关请求", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@RestController
@RequestMapping("/cms/coupon")
public class CmsCouponController extends BaseController {
    @Resource
    private CmsCouponService cmsCouponService;

    @PostMapping("/save")
    @ApiOperation(httpMethod = "POST", value = "新增一条记录")
    public Wrapper add(@ApiParam(name = "cmsCoupon", value = "该数据记录的POJO") @RequestBody CmsCoupon cmsCoupon) {
        cmsCoupon.setUpdateUser(getLoginAuthDto().getUserId());
        cmsCoupon.setGroupId(getLoginAuthDto().getGroupId());
        cmsCouponService.save(cmsCoupon);
        return WrapMapper.ok();
    }

    @DeleteMapping("/delete/{id}")
    @ApiOperation(httpMethod = "DELETE", value = "根据id删除一条记录")
    public Wrapper delete(@ApiParam(name = "id", value = "记录id") @PathVariable Long id){
        cmsCouponService.deleteById(id);
        return WrapMapper.ok();
    }

    @PutMapping("/update")
    @ApiOperation(httpMethod = "PUT", value = "修改一条记录")
    public Wrapper update(@ApiParam(name = "cmsCoupon", value = "需要修改的内容") @RequestBody CmsCoupon cmsCoupon) {
        cmsCoupon.setUpdateUser(getLoginAuthDto().getUserId());
        cmsCouponService.update(cmsCoupon);
        return WrapMapper.ok();
    }

    @GetMapping("/{id}")
    @ApiOperation(httpMethod = "GET", value = "根据ID获取具体信息")
    public Wrapper<CmsCoupon> detail(@ApiParam(name = "id", value = "记录id") @PathVariable Long id) {
        CmsCoupon cmsCoupon = cmsCouponService.findById(id);
        return WrapMapper.ok(cmsCoupon);
    }

    @GetMapping
    @ApiOperation(httpMethod = "GET", value = "分页获取所有记录")
    public Wrapper<PageInfo<CmsCoupon>> list(@ApiParam(name = "page", value = "分页值", defaultValue = "0") @RequestParam(defaultValue = "0") Integer page,
                                             @ApiParam(name = "size", value = "每分页返回的记录数", defaultValue = "0") @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        Example example = new Example(CmsCoupon.class);
        Example.Criteria criteria = example.createCriteria();
        if (getGroupInfoDto().getGroupId() != null && getGroupInfoDto().getGroupId()>1) {
            criteria.andEqualTo("groupId", getGroupInfoDto().getGroupId());
        }
        List<CmsCoupon> list = cmsCouponService.selectByExample(example);
        PageInfo pageInfo = new PageInfo(list);
        return WrapMapper.ok(pageInfo);
    }

    /**
     * 可领优惠券-我的页面-优惠券领取
     * @return
     */
    @GetMapping("/list/coupon")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper< List<CmsCoupon>> list(@ApiParam(name = "userId", value = "记录userId") @RequestParam Long userId,
                                          @ApiParam(name = "sendAspect", value = "记录sendAspect") @RequestParam(defaultValue = "0")  Integer sendAspect ) {
        System.out.println("可领优惠券1");
        Long groupId = getGroupInfoDto().getGroupId();
        List<CmsCoupon> list = cmsCouponService.getCouponList(userId,sendAspect,groupId);
        return WrapMapper.ok(list);
    }

    /**
     * 可领优惠券
     * @return
     */
    @GetMapping("/list/{userId}")
    @ApiOperation(httpMethod = "GET", value = "获取所有记录")
    public Wrapper< List<CmsCoupon>> list(@ApiParam(name = "userId", value = "记录userId") @PathVariable Long userId) {
        System.out.println("可领优惠券2");
        Integer sendAspect = 1;
        Long groupId = getGroupInfoDto().getGroupId();
        List<CmsCoupon> list = cmsCouponService.getCouponList(userId,sendAspect,groupId);
        return WrapMapper.ok(list);
    }

    /**
     * 获取所有红包
     * @return
     */
    @PostMapping("/getRedCouponList")
    @ApiOperation(httpMethod = "POST", value = "获取所有红包")
    public Wrapper< List<CmsCoupon>> getRedCouponlist(@ApiParam(name = "cmsCoupon", value = "cmsCoupon") @RequestBody CmsCoupon cmsCoupon) {
        if (cmsCoupon == null || cmsCoupon.getSendType() == null){
            return WrapMapper.error("参数错误");
        }
        if (cmsCoupon.getGroupId() == null){
            cmsCoupon.setCouponId(getGroupInfoDto().getGroupId());
        }

        List<CmsCoupon> list = cmsCouponService.getRedCouponlist(cmsCoupon);
        return WrapMapper.ok(list);
    }
}
